// Server API makes it possible to hook into various parts of Gridsome
// on server-side and add custom data to the GraphQL data layer.
// Learn more: https://gridsome.org/docs/server-api/

// Changes here require a server restart.
// To restart press CTRL + C in terminal and run `gridsome develop`
const axios = require('axios')
const config = {
  id: 1,
  "githubUsername": "GitHub-Laziji",
  "blogTitle": "Laziji",
  "blogDescribe": "欢迎来到辣子鸡(fork)的个人博客。",
  "htmlTitle": "辣子鸡(fork)的博客",
  "fontColor": "#ffffff",
  "useBackgroundImage": false,
  "backgroundColorLeft": "#2690F9",
  "backgroundColorRight": "#FC2D2D",
  "audioUrl": "http://sc1.111ttt.cn:8282/2018/1/03m/13/396131232171.m4a?tflag=1519095601&pin=6cd414115fdb9a950d827487b16b5f97#.mp3",
  "mini": false,
  audioAutoPlay: false,
  "webSites":[
    {"name":"v2ex" , "url":"https://www.v2ex.com/member/laziji"}
  ]
}

async function loadUser (actions, nodeName) {
  const collection = actions.addCollection(nodeName)

  const { data } = await axios.get(`https://api.github.com/users/${config.githubUsername}`)
  console.log(data)
  collection.addNode(data)

}

function loadConf (actions, nodeName) {
  const collection = actions.addCollection(nodeName)
  collection.addNode(config)
}

module.exports = function (api) {
  api.loadSource(async actions => {
    // Use the Data Store API here: https://gridsome.org/docs/data-store-api/
    await loadUser(actions, 'user')
    loadConf(actions, 'conf')
  })

  api.createPages(({ createPage }) => {
    // Use the Pages API here: https://gridsome.org/docs/pages-api/
  })
}


// module.exports = function (api) {
//   api.loadSource(async actions => {
//     const collection = actions.addCollection('Post')
//
//     const { data } = await axios.get('https://api.example.com/posts')
//
//     for (const item of data) {
//       collection.addNode({
//         id: item.id,
//         title: item.title,
//         content: item.content
//       })
//     }
//   })
// }
